<?php

class CommentsController extends Controller
{
	public function actionAdd()
	{
		$oComments = new Comments();

		$params = $this->_getParams();

			$params['user_id'] = $this->_userInfo ? $this->_userInfo->id : null;

		Messages::getInst()->clear();
		if(empty($params['target_id']) || (empty($params['user_id']) && empty($params['user_name']))) //!mb_eregi('[a-z][a-z0-9]+\.?[a-z0-9]+@([a-z0-9]+\.?)+[a-z](2,)', $params['email'])
		{
			Messages::getInst()->addMessage('error', _('Введите свой email'));

			if(!empty($params['comment']))
				Yii::app()->request->cookies['comment'] = new CHttpCookie('comment', $params['comment'], array(
                                                                                                                'httpOnly' => true,
                                                                                                                'expire' => time() + 86400,
                                                                                                                'path' => $_SERVER['REQUEST_URI']
                                                                                                           ));

			return $this->redirect($_SERVER['HTTP_REFERER'].'#review');
		}

		$oString = new String();

		$params['user_name'] = $oString->stripTagsAttributes($params['user_name']);

		if(!empty($params['email']))
			$params['email'] = $oString->stripTagsAttributes($params['email']);

		$params['target_type'] = 'goods';
		$params['date_add'] = date('Y-m-d H:i:s');
		$params['status'] = 'postmoderation';
		$params['ip'] = $_SERVER['REMOTE_ADDR'];
		$params['user_agent'] = $_SERVER['HTTP_USER_AGENT'];

		$oComments->setAttributes($params);

		$oComments->save();

		Yii::app()->request->cookies['comment_success'] = new CHttpCookie('comment_success', 1, array(
		                                                                               'httpOnly' => true,
		                                                                               'expire' => time() + 86400,
		                                                                               'path' => '/'
		                                                                          ));

		return $this->redirect($_SERVER['HTTP_REFERER'].'#review');
	}
}